import * as string from './types/string.js'

export default class ArgsSchema {
    #definition
    #flags
    #defaults

    constructor(definition) {
        this.#definition = definition
        this.#flags = Object.keys(this.#definition).filter(key => key.length === 1)
        this.#defaults = Object.fromEntries(this.#flags.map(flag => [ flag, this.defaultOf(flag) ]))
    }

    get flags() {
        return this.#flags
    }

    get defaults() {
        return this.#defaults
    }

    defined(flag) {
        return this.flags.includes(flag)
    }

    definitionOf(flag) {
        if (!this.defined(flag)) throw `Undefined flag: ${JSON.stringify(flag)}`
        return this.#definition[flag]
    }

    typeOf(flag) {
        return this.definitionOf(flag).type ?? string
    }

    defaultOf(flag) {
        return this.definitionOf(flag).default ?? this.typeOf(flag).globalDefault
    }

    descriptionOf(flag) {
        return this.definitionOf(flag).description ?? '(No description)'
    }
}
